package xcmg.device.service.warehouse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.initialInventory.InitialInventoryDetailDO;
import xcmg.device.dao.mapper.initialInventory.InitialInventoryDetailMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.initialInventory.InitialInventoryDetailVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import java.util.*;

/**
 * @author
 * @create 2018-03-20 19:17
 **/
@Service
public class InitialInventoryDetailService
{
    @Autowired
    private InitialInventoryDetailMapper initialInventoryDetailMapper;

    @Autowired
    private FastGenClient fastGenClient;

    /**
     * 添加期初入库零件添加
     * @param detailVOs List<InitialInventoryDetailVO> 零件信息
     * @param billNo String 期初入库单号
     * @param billId String 期初入库单ID
     * @param orgId String 当前企业ID
     * @return ErrorCode 返回错误码
     * @throws Exception Exception
     */
    public ErrorCode add(List<InitialInventoryDetailVO> detailVOs, String billNo, String billId, String orgId,String location,String userId,String userName) throws Exception
    {
        for (InitialInventoryDetailVO detailVO : detailVOs)
        {
            ActionResult<String> textResult = fastGenClient.textGuid();
            if (null == textResult || textResult.getCode() != 0)
            {
                throw new Exception("生成期初入库单ID失败!");
            }
            String id = textResult.getValue();
            InitialInventoryDetailDO initialInventoryDetailDO = new InitialInventoryDetailDO();
            BeanUtils.copyProperties(detailVO, initialInventoryDetailDO);
            initialInventoryDetailDO.setOrgId(orgId);
            initialInventoryDetailDO.setMainId(billId);
            initialInventoryDetailDO.setMainNo(billNo);
            initialInventoryDetailDO.setLocation(location);
            initialInventoryDetailDO.setCreateName(userName);
            initialInventoryDetailDO.setCreateId(userId);
            initialInventoryDetailDO.setId(id);
            initialInventoryDetailDO.setLocation(location);
            initialInventoryDetailDO.setState(0);
           // initialInventoryDetailDO.setUnitPrice();
            int ret = initialInventoryDetailMapper.insert(initialInventoryDetailDO);
            if (ret <= 0)
            {
                throw new Exception("添加期初入库零件信息异常！");
            }
        }
        return ErrorCode.Success;
    }




}
